package com.peanut.baby.widget.im;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.InitManager;


public class EaseChatRowVoice extends EaseChatRowFile{
    private static final int MAX_AUDIO_TIME_SECOND = 120;

    private ImageView voiceImageView;
    private TextView voiceLengthView;
    private ImageView readStatusView;

    public EaseChatRowVoice(Context context, int position, BaseAdapter adapter) {
        super(context, position, adapter);
    }

    @Override
    protected void onInflateView() {
//        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
//                R.layout.ease_row_received_voice : R.layout.ease_row_sent_voice, this);
    }

    @Override
    protected void onFindViewById() {
//        voiceImageView = ((ImageView) findViewById(R.id.iv_voice));
//        voiceLengthView = (TextView) findViewById(R.id.tv_length);
//        readStatusView = (ImageView) findViewById(R.id.iv_unread_voice);
    }

    @Override
    protected void onSetUpView() {
//        EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) message.getBody();
//        int len = voiceBody.getLength();
//        setAudioBubbleWidth(bubbleLayout,len);
//        if(len>0){
//            voiceLengthView.setText(voiceBody.getLength() + "\"");
//            voiceLengthView.setVisibility(View.VISIBLE);
//        }else{
//            voiceLengthView.setVisibility(View.INVISIBLE);
//        }
//        if (EaseChatRowVoicePlayClickListener.playMsgId != null
//                && EaseChatRowVoicePlayClickListener.playMsgId.equals(message.getMsgId()) && EaseChatRowVoicePlayClickListener.isPlaying) {
//            AnimationDrawable voiceAnimation;
//            if (message.direct() == EMMessage.Direct.RECEIVE) {
//                voiceImageView.setImageResource(R.drawable.voice_from_icon);
//            } else {
//                voiceImageView.setImageResource(R.drawable.voice_to_icon);
//            }
//            voiceAnimation = (AnimationDrawable) voiceImageView.getDrawable();
//            voiceAnimation.start();
//        } else {
//            if (message.direct() == EMMessage.Direct.RECEIVE) {
//                voiceImageView.setImageResource(R.drawable.ease_chatfrom_voice_playing_f3);
//            } else {
//                voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing_f3);
//            }
//        }
//
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            if (message.isListened()) {
//                // hide the unread icon
//                readStatusView.setVisibility(View.INVISIBLE);
//            } else {
//                readStatusView.setVisibility(View.VISIBLE);
//            }
//            EMLog.d(TAG, "it is receive msg");
//            if (voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
//                    voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
//                progressBar.setVisibility(View.VISIBLE);
//                setMessageReceiveCallback();
//            } else {
//                progressBar.setVisibility(View.INVISIBLE);
//
//            }
//            return;
//        }
//
//        // until here, handle sending voice message
//        handleSendMessage();
    }

    public static int getAudioMaxEdge() {
        return (int) (0.6 * InitManager.getInstance().getScreenWidth());
    }

    public static int getAudioMinEdge() {
        return (int) (0.1875 * InitManager.getInstance().getScreenWidth());
    }

    public static void setAudioBubbleWidth(View bubbleLayout, int seconds) {
        int currentBubbleWidth = calculateBubbleWidth(seconds, MAX_AUDIO_TIME_SECOND);
        ViewGroup.LayoutParams layoutParams = bubbleLayout.getLayoutParams();
        layoutParams.width = currentBubbleWidth;
        bubbleLayout.setLayoutParams(layoutParams);
    }



    public static int calculateBubbleWidth(long seconds, int MAX_TIME) {
        int maxAudioBubbleWidth = getAudioMaxEdge();
        int minAudioBubbleWidth = getAudioMinEdge();

        int currentBubbleWidth;
        if (seconds <= 0) {
            currentBubbleWidth = minAudioBubbleWidth;
        } else if (seconds > 0 && seconds <= MAX_TIME) {
            currentBubbleWidth = (int) ((maxAudioBubbleWidth - minAudioBubbleWidth) * (2.0 / Math.PI)
                    * Math.atan(seconds / 10.0) + minAudioBubbleWidth);
        } else {
            currentBubbleWidth = maxAudioBubbleWidth;
        }

        if (currentBubbleWidth < minAudioBubbleWidth) {
            currentBubbleWidth = minAudioBubbleWidth;
        } else if (currentBubbleWidth > maxAudioBubbleWidth) {
            currentBubbleWidth = maxAudioBubbleWidth;
        }

        return currentBubbleWidth;
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onBubbleClick() {
//        new EaseChatRowVoicePlayClickListener(message, voiceImageView, readStatusView, adapter, activity).onClick(bubbleLayout);
    }
    
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (EaseChatRowVoicePlayClickListener.currentPlayListener != null && EaseChatRowVoicePlayClickListener.isPlaying) {
            EaseChatRowVoicePlayClickListener.currentPlayListener.stopPlayVoice();
        }
    }
    
}
